import 'package:flutter/material.dart';
import 'package:flutter_module/viewModels/layer_edit_viewmodel.dart';
import 'package:provider/provider.dart';

class LayerItemInput extends StatelessWidget {
  final LayerEditViewModel model;

  const LayerItemInput(this.model, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        const Text("是否作为地图标注项"),
        Row(
          children: [
            Expanded(
              child: Selector<LayerEditViewModel, bool>(
                selector: (context, provider) => provider.curItem.isMainName,
                builder: (context, value, child) {
                  return RadioListTile(
                    title: const Text("否"),
                    value: false,
                    groupValue: model.curItem.isMainName,
                    onChanged: (value) {
                      model.setItemMainName(false);
                    },
                  );
                },
              ),
            ),
            Expanded(
              child: Selector<LayerEditViewModel, bool>(
                selector: (context, object) => object.curItem.isMainName,
                builder: (context, value, child) {
                  return RadioListTile(
                    title: const Text("是"),
                    value: true,
                    groupValue: model.curItem.isMainName,
                    onChanged: (value) {
                      if (model.isHisMainName()) {
                        _showMainNameDialog(context);
                      } else {
                        model.setItemMainName(true);
                      }
                    },
                  );
                },
              ),
            ),
          ],
        ),
        Row(
          children: [
            Expanded(
              child: Selector<LayerEditViewModel, bool>(
                selector: (context, provider) => provider.curItem.nullable,
                builder: (context, value, child) {
                  return RadioListTile(
                    title: const Text("必填"),
                    value: false,
                    groupValue: model.curItem.nullable,
                    onChanged: (value) {
                      model.setItemNullable(false);
                    },
                  );
                },
              ),
            ),
            Expanded(
              child: Selector<LayerEditViewModel, bool>(
                selector: (context, object) => object.curItem.nullable,
                builder: (context, value, child) {
                  return RadioListTile(
                    title: const Text("非必填"),
                    value: true,
                    groupValue: model.curItem.nullable,
                    onChanged: (value) {
                      model .setItemNullable(true);
                    },
                  );
                },
              ),
            ),
          ],
        ),
      ],
    );
  }

  void _showMainNameDialog(BuildContext context) {
    showDialog(
        barrierDismissible: false,
        context: context,
        builder: (context) {
          return AlertDialog(
            title: const Text("提示"),
            content: const Text("已经存在一条属性做为地图渲染名称，是否替换？"),
            actions: <Widget>[
              TextButton(
                onPressed: () {
                  model.setItemMainName(false);
                  Navigator.of(context).pop('cancel');
                },
                child: const Text("取消"),
              ),
              TextButton(
                onPressed: () {
                  model.setItemMainName(true);
                  Navigator.of(context).pop('ok');
                },
                child: const Text("确定"),
              ),
            ],
          );
        });
  }
}
